package br.com.spacestar.services
{
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	
	import mx.core.FlexGlobals;
	import mx.managers.SystemManager;
	
	import sk.yoz.events.FacebookOAuthGraphEvent;
	import sk.yoz.net.FacebookOAuthGraph;

	public class FacebookService
	{
		private var clientId:String = "129281950466958";
		
		private var redirectURI:String = "http://www.optimedia.com.br/Virtuoso/callback.html";
		
		private var scope:String = "publish_stream,user_photos,user_photo_video_tags";
		
		private var fb:FacebookOAuthGraph;
		
		public function autoConnect(callback:Function):void {
			fb = new FacebookOAuthGraph();
			fb.clientId = clientId;
			fb.redirectURI = redirectURI;
			fb.scope = scope;
			fb.useSecuredPath = true;
			fb.addEventListener(FacebookOAuthGraphEvent.AUTHORIZED, callback);
			
			fb.autoConnect(FlexGlobals.topLevelApplication.systemManager.loaderInfo.parameters);
		}
		
		public function connect(callback:Function):void {
			fb.connect();
		}
		
		public function call(path:String, callback:Function, binary:Boolean=false):void {
			var loader:URLLoader = fb.call( path );
			loader.dataFormat = binary?URLLoaderDataFormat.BINARY:URLLoaderDataFormat.TEXT;
			loader.addEventListener(FacebookOAuthGraphEvent.DATA, callback);
		}
		
	}
}